/*
 * Copyright (C) 2011 Consulanza Informatica.
 * info@consulanza.it
 *
 * This file is part of Customer Calls Tracker.
 *
 *    Customer Calls Tracker is free software: you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or (at 
 *    your option) any later version.
 *
 *    Customer Calls Tracker is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 *    Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Customer Calls Tracker. If not, see http://www.gnu.org/licenses/.
 *
 */

package consulanza.amlsoft.customercalls;


import consulanza.amlsoft.customercalls.CustomerCallsDbAdapter.CustomerCall;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Filterable;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CustomerCallsCursorAdapter extends SimpleCursorAdapter implements Filterable {
   // private final String TAG = "CustomerCallsCursorAdapter";
	//private Context context;
    private int layout;
    /**
     * 
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     */
    public CustomerCallsCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        // this.context = context;
        this.layout = layout;
    }
    /**
     * get string value from cursor column
	 * @param cursor
	 * @param colName
     */
    private String getCursorString(Cursor cursor, String colName) {
        int columnIndex = cursor.getColumnIndex(colName);
        return cursor.getString(columnIndex);   	
    }
    /**
     * get string value from cursor column
	 * @param cursor
	 * @param colName
     */
    private int getCursorInt(Cursor cursor, String colName) {
        int columnIndex = cursor.getColumnIndex(colName);
        return cursor.getInt(columnIndex);   	
    }
    /**
     * 
     * @param view
     * @param cursor
     * @param colName
     * @param fieldId
     */
    private void bindViewTextField(View view, Cursor cursor, String colName, int fieldId) {
        String value = getCursorString(cursor, colName);
        TextView field = (TextView) view.findViewById(fieldId);
        if (field != null) {
            field.setText(value);
        }    	
    }
    /**
     * 
     * @param view
     * @param cursor
     * @param colName
     * @param dowFieldId
     * @param timeFieldId
     */
    private void bindViewDateField(View view, Cursor cursor, String colName, int dowFieldId, int timeFieldId) {
        String value = getCursorString(cursor, colName);
        TextView dowField = (TextView) view.findViewById(dowFieldId);
        if (dowField != null) {
            dowField.setText(Util.dateToDow(Util.dbDateToDate(value)));
        }
        TextView timeField = (TextView) view.findViewById(timeFieldId);
        if (timeField != null) {
            timeField.setText(Util.dateToString(Util.dbDateToDate(value)));
        }    	
    }
    /**
     * 
     * @param view
     * @param cursor
     * @param colName
     * @param fieldId
     */
    private void bindViewCheckboxField(View view, Cursor cursor, String colName, int fieldId) {
        Boolean value = getCursorInt(cursor, colName) > 0;
        CheckBox field = (CheckBox) view.findViewById(fieldId);
        if (field != null) {
            field.setChecked(value);
        }    	
    }
    /**
     * 
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        Cursor c = getCursor();
        final LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(layout, parent, false);
        
    	// customer
    	bindViewTextField(view, c, CustomerCall.KEY_CUSTOMER, R.id.row_customer);
    	// notes
    	bindViewTextField(view, c, CustomerCall.KEY_NOTES, R.id.row_notes);
    	// start date
    	bindViewDateField(view, c, CustomerCall.KEY_START, R.id.row_dowstart,R.id.row_timestart);
    	// end date
    	bindViewDateField(view, c, CustomerCall.KEY_END, R.id.row_dowend,R.id.row_timeend);
    	// flag call
    	bindViewCheckboxField(view, c, CustomerCall.KEY_FLAG_CALL, R.id.row_flag_call);
    	// flag billed
    	bindViewCheckboxField(view, c, CustomerCall.KEY_FLAG_BILLED, R.id.row_flag_billed);
    	// total time
    	String timeStart = getCursorString(c, CustomerCall.KEY_START);
    	String timeEnd = getCursorString(c, CustomerCall.KEY_END);
        TextView mTimeTot = (TextView) view.findViewById(R.id.row_hourstot);
        if (mTimeTot != null) {
        	mTimeTot.setText(Util.timeDiff(timeStart,timeEnd));
        }
        
        return view;
    }
    /**
     * bind View fields to Cursor fields
     * @param view
     * @param context
     * @param cursor
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    	// customer
    	bindViewTextField(view, cursor, CustomerCall.KEY_CUSTOMER, R.id.row_customer);
    	// notes
    	bindViewTextField(view, cursor, CustomerCall.KEY_NOTES, R.id.row_notes);
    	// start date
    	bindViewDateField(view, cursor, CustomerCall.KEY_START, R.id.row_dowstart,R.id.row_timestart);
    	// end date
    	bindViewDateField(view, cursor, CustomerCall.KEY_END, R.id.row_dowend,R.id.row_timeend);
    	// flag call
    	bindViewCheckboxField(view, cursor, CustomerCall.KEY_FLAG_CALL, R.id.row_flag_call);
    	// flag billed
    	bindViewCheckboxField(view, cursor, CustomerCall.KEY_FLAG_BILLED, R.id.row_flag_billed);
    	// total time
    	String timeStart = getCursorString(cursor, CustomerCall.KEY_START);
    	String timeEnd = getCursorString(cursor, CustomerCall.KEY_END);
        TextView mTimeTot = (TextView) view.findViewById(R.id.row_hourstot);
        if (mTimeTot != null) {
        	mTimeTot.setText(Util.timeDiff(timeStart,timeEnd));
        }
    }
}
